﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubTouchCtrl : MonoBehaviour
{
        [Header("控制旋转的脚本")]
        public CubRotate cr;

        [Header("控制炸开的脚本")]
        public CubExplode ce;

        [Header("判断阈值")]
        public float length = 0.5f;

        Vector2 posBegin;
        Vector2 posIng;

        public bool isTouch;

        private bool isEnable;

        void Start()
        {
                isTouch = true;
        }

        //鼠标在当前控件的按下事件
        public void TouchDown()
        {
                isEnable = true;
                Debug.Log("按下");
                //获取初始坐标
                posBegin = TouchPos();

                //不赋值导致刚刚按下的时候旋转几圈
                mouse = posBegin;
        }

        //鼠标在当前控件的抬起事件
        public void TouchUp()
        {
                Debug.Log("抬起");
                //如果是点击
                if (ce && isTouch)
                {
                        Debug.Log("此为点击事件");
                        ce.Touch();
                }

                isTouch = true;
                isEnable = false;
        }

        void Update()
        {
                if (isEnable)
                {
                        if ( isTouch)
                        {
                                Debug.Log("拖动");
                                posIng = TouchPos();
                                if ((posBegin - posIng).magnitude > length)
                                {
                                        isTouch = false;
                                }
                        }

                        //如果是拖动旋转了
                        if (!isTouch)
                        {
                                cr.TouchRotate(TouchPosMove());
                        }
                }
        }

        Vector2 mouse;
        /// <summary>
        /// 用这个函数以避免旋转乱问题
        /// </summary>
        /// <returns></returns>
        public Vector2 TouchPosMove()
        {
                Vector2 p = new Vector2();
                if (Input.GetMouseButton(0))
                {
                        p = new Vector2(Input.mousePosition.x, Input.mousePosition.y);

                }
                else if (Input.touchCount > 0)
                {
                        p = new Vector2(Input.touches[0].deltaPosition.x, Input.touches[0].deltaPosition.y);
                }

                Vector2 r = p - mouse;
                mouse = p;
                return r;
        }

        //用这个函数以避免点击不了的问题
        public Vector2 TouchPos()
        {
                if (Input.GetMouseButton(0))
                {
                        return new Vector2(Input.mousePosition.x, Input.mousePosition.y);
                }
                else if (Input.touchCount > 0)
                {
                        return new Vector2(Input.touches[0].deltaPosition.x, Input.touches[0].deltaPosition.y);
                }
                return new Vector2();
        }
        //开始触摸
        public bool TouchBegin()
        {
                if (Input.GetMouseButtonDown(0))
                {
                        return true;
                }
                else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
                {
                        return true;
                }
                return false;
        }
        //结束触摸
        public bool TouchEnd()
        {
                if (Input.GetMouseButtonUp(0))
                {
                        return true;
                }
                else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
                {
                        return true;
                }
                return false;

        }
        //触摸过程
        public bool TouchIng()
        {
                if (Input.GetMouseButton(0))
                {
                        return true;
                }
                else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                        return true;
                }
                return false;
        }
}
